package com.ddwl.data.dao;

import com.ddwl.common.constant.OperateLogEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.schema.bo.data.OperateLogBo;
import com.ddwl.data.mapper.OperateLogMapper;
import com.ddwl.data.model.OperateLog;
import com.ddwl.schema.bo.user.UserSearchBo;
import com.ddwl.schema.util.DateUtil;
import com.ddwl.schema.vo.order.StatisticsUserVo;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;
import java.util.Map;

/**
 * @Author hemiaojun
 * @Date 2020/9/17 11:42
 */

@Repository
public class OperateLogDao extends TkDao<OperateLogMapper,OperateLog> {

    @Resource
    private OperateLogMapper operateLogMapper;

    public Integer countOperateLog(Integer code, String key, String method) {
        Example example=getExample();
        example.and().andEqualTo("method", method)
                .andGreaterThanOrEqualTo("operateTime", DateUtil.getTimesMorning())
                .andEqualTo(OperateLogEnum.SearchPropertyEnum.getSearchProperty(code), key);
        return operateLogMapper.selectCountByExample(example);
    }

    public List<OperateLog> getList(OperateLogBo param) {
        Example example = getExample();
        example.and().andIsNotNull("operateId")
                .andEqualTo("pk", param.getPk());
        return operateLogMapper.selectByExample(example);
    }

    public List<StatisticsUserVo> queryActiveUserCount(UserSearchBo bo) {
        return baseMapper.queryActiveUserCount(bo);
    }

    public void changeId(Map<String, String> map) {
        operateLogMapper.changeId(map);
    }

    public List<OperateLog> getListByPage() {
        Example example = new Example(OperateLog.class);
        return operateLogMapper.selectByExample(example);
    }

    public void deleteBeforeSpecifyDateOperateLog(Instant date) {
        Example example = new Example(OperateLog.class);
        example.and().andLessThanOrEqualTo("gmtCreated", date);
        operateLogMapper.deleteByExample(example);
    }
}
